package com.uchedao.seller.ui.car.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.uchedao.seller.R;
import com.uchedao.seller.config.Api;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.http.request.HttpRequest;
import com.uchedao.seller.http.response.ForSaleListResponse;
import com.uchedao.seller.ui.car.adapter.CarForSaleAdapter;
import com.uchedao.seller.ui.car.base.TabBaseFragment;
import com.uchedao.seller.util.T;
import com.uchedao.seller.widget.library.PullToRefreshBase;
import com.uchedao.seller.widget.library.PullToRefreshListView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hang on 2015/10/22.
 * 待售车辆
 */
public class CarForSaleFragment extends TabBaseFragment implements PullToRefreshBase.OnRefreshListener2 {

    private PullToRefreshListView lvData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRoot = inflater.inflate(R.layout.fragment_for_sale, container, false);
        initView();
        return mRoot;
    }

    @Override
    public void onStart() {
        super.onStart();
        loadData();
    }

    @Override
    protected String getActivityTag() {
        return "CarForSaleFragment";
    }

    public void initView() {
        lvData = (PullToRefreshListView)mRoot.findViewById(R.id.lvCarBidEnd);
        lvData.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lvData.setOnRefreshListener(this);
    }

    public void loadData() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("app_name", GlobalInfo.APP_NAME);
        Request req = HttpRequest.getRequest(HttpRequest.GET, Api.Action.WAIT_CAR_LIST, map,
                new Response.Listener<JsonObject>() {
                    @Override
                    public void onResponse(JsonObject response) {
                        lvData.onRefreshComplete();
                        ForSaleListResponse entity = new Gson().fromJson(response, ForSaleListResponse.class);
                        lvData.setAdapter(new CarForSaleAdapter(mContext, entity.getList()));
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        lvData.onRefreshComplete();
                        T.showShort(mContext, error.getMessage());
                    }
                }, true);
        addQueue(req);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
    }
}
